package com.kmxd.ams.controller.archive;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kmxd.ams.client.archive.IArchiveAuthRuleSvc;
import com.kmxd.ams.client.archive.dto.ArchiveAuthRuleAddDTO;
import com.kmxd.ams.client.archive.dto.ArchiveAuthRuleEditDTO;
import com.kmxd.ams.client.archive.dto.ArchiveAuthRuleQueryDTO;
import com.kmxd.ams.client.archive.vo.ArchiveAuthRuleListVO;
import com.kmxd.ams.client.archive.vo.ArchiveAuthRuleVO;
import com.kmxd.ams.core.common.enums.LogLevelEnum;
import com.kmxd.ams.core.common.enums.LogTypeEnum;
import com.kmxd.ams.core.log.annotation.AutoLog;
import com.kmxd.ams.core.result.R;
import com.kmxd.ams.core.util.SecurityUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import javax.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 特殊利用授权 前端控制器
 *
 * @author nickbi
 * @since 2022-07-27
 */
@RestController
@Tag(name = "特殊利用授权")
@RequestMapping("/archive/archiveAuthRule")
@AllArgsConstructor
public class ArchiveAuthRuleCtl {

  private IArchiveAuthRuleSvc archiveAuthRuleService;

  /** 分页查询 */
  @Operation(summary = "分页查询")
  @GetMapping("/selectPage")
  @AutoLog(module = "特殊利用授权", type = LogTypeEnum.SYSTEM, title = "分页查询")
  public R<Page<ArchiveAuthRuleListVO>> selectPage(Page reqPage, ArchiveAuthRuleQueryDTO req) {
    return R.success(archiveAuthRuleService.selectPage(reqPage, req));
  }

  /** 新增 */
  @Operation(summary = "新增")
  @PostMapping("/add")
  @AutoLog(module = "特殊利用授权", type = LogTypeEnum.SYSTEM, title = "新增")
  public R<Void> add(@Validated @RequestBody ArchiveAuthRuleAddDTO req) {
    req.setFondsId(
        ObjectUtil.isNotEmpty(req.getFondsId()) ? req.getFondsId() : SecurityUtil.getFondsId());
    req.setUserScope(ObjectUtil.isNotEmpty(req.getUserScope()) ? req.getUserScope() : 2);
    req.setStatus(ObjectUtil.isNotEmpty(req.getStatus()) ? req.getStatus() : true);
    archiveAuthRuleService.add(req);
    return R.success();
  }

  /** 修改 */
  @Operation(summary = "修改")
  @PostMapping("/edit")
  @AutoLog(module = "特殊利用授权", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.HIGH, title = "修改")
  public R<Void> edit(@Validated @RequestBody ArchiveAuthRuleEditDTO req) {
    req.setFondsId(
        ObjectUtil.isNotEmpty(req.getFondsId()) ? req.getFondsId() : SecurityUtil.getFondsId());
    req.setUserScope(ObjectUtil.isNotEmpty(req.getUserScope()) ? req.getUserScope() : 2);
    req.setStatus(ObjectUtil.isNotEmpty(req.getStatus()) ? req.getStatus() : true);
    archiveAuthRuleService.edit(req);
    return R.success();
  }

  /** 删除 */
  @Operation(summary = "删除")
  @PostMapping("/remove")
  @AutoLog(
      module = "特殊利用授权",
      type = LogTypeEnum.SYSTEM,
      level = LogLevelEnum.HIGH,
      title = "删除")
  public R<Void> remove(@RequestParam String ids) {
    archiveAuthRuleService.remove(ids);
    return R.success();
  }

  /** 查看 */
  @Operation(summary = "查看")
  @GetMapping("/view")
  @AutoLog(module = "特殊利用授权", type = LogTypeEnum.SYSTEM, title = "查看")
  public R<ArchiveAuthRuleVO> view(@RequestParam String id) {
    return R.success(archiveAuthRuleService.view(id));
  }
  /** 导出 */
  @Operation(summary = "导出")
  @GetMapping("/export")
  @AutoLog(module = "特殊利用授权", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.HIGH, title = "导出")
  @SneakyThrows
  public void export(ArchiveAuthRuleQueryDTO req, HttpServletResponse response) {
    String filename = "特殊利用授权列表" + "_" + System.currentTimeMillis() + ".xlsx";
    response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("UTF-8");
    response.setHeader(
        "Content-Disposition",
        "attachment;filename=" + URLEncoder.encode(filename, StandardCharsets.UTF_8.name()));
    EasyExcel.write(response.getOutputStream())
        .head(ArchiveAuthRuleListVO.class)
        .sheet("特殊利用授权列表")
        .doWrite(archiveAuthRuleService.selectList(req));
  }
}
